package cn.wyl.user.service.impl;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.basic.util.Md5Utils;
import cn.wyl.basic.util.StrUtils;
import cn.wyl.user.domain.Logininfo;
import cn.wyl.user.domain.User;
import cn.wyl.user.mapper.LogininfoMapper;
import cn.wyl.user.mapper.UserMapper;
import cn.wyl.user.service.IUserService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void phoneRegister(Map<String, String> map) {
        String phone = map.get("phone");
        String phoneCode = map.get("phoneCode");
        String password = map.get("password");
        String passwordRepeat = map.get("passwordRepeat");

        //1.校验 输入信息是否为空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)
        ||StringUtils.isEmpty(password) ||StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("输入信息不能为空");
        }
        //2.校验 两次密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致");
        }
        //3.校验 手机验证码是否过期
        Object obj = redisTemplate.opsForValue().get("register:" + phone);
        if(obj == null){
            throw new BusinessException("验证码已过期，请重新获取");
        }
        if(!obj.toString().split(":")[0].equals(phoneCode)){
            throw new BusinessException("验证码错误，请核对后重新输入");
        }
        //4.添加到数据库
        User user = map2User(phone,password);
        Logininfo logininfo = user2Logininfo(user);
        //4.1 先添加到t_logininfo表
        logininfoMapper.save(logininfo);
        //4.2 添加到t_user表
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //底层会按照属性同名原则进行属性的拷贝：id,username,phone,email,salt,password
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//1表示用户
        logininfo.setDisable(true);
        return logininfo;
    }

    private User map2User(String phone, String password) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5Pwd = Md5Utils.encrypByMd5(password + salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }

    @Override
    public void delete(Long id) {
        //查询user对象
        User user = userMapper.findById(id);
        //删除logininfo信息
        logininfoMapper.delete(user.getLogininfoId());
        //删除user信息
        super.delete(id);
    }

    @Override
    public void update(User user) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);

        //查询到原来的user对象
        User oldUser = userMapper.findById(user.getId());
        user.setLogininfoId(oldUser.getLogininfoId());

        //更新数据t_user
        super.update(user);

        //更新数据t_logininfo
        Logininfo oldLogininfo = logininfoMapper.findById(oldUser.getLogininfoId());
        BeanUtils.copyProperties(user,oldLogininfo);
        //属性同名原则会把员工的id拷贝给登录信息对象的id
        oldLogininfo.setId(oldUser.getLogininfoId());
        logininfoMapper.update(oldLogininfo);
    }

    @Override
    public void emailRegister(Map<String, String> map) {
        String email = map.get("email");
        String password = map.get("password");
        String passwordRepeat = map.get("passwordRepeat");

        //1.校验 输入信息是否为空
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("输入信息不能为空");
        }
        //2.校验 两次密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致");
        }
        //3.添加到数据库
        User user = mapEmailUser(email,password);
        Logininfo logininfo = user2Logininfo(user);
        //3.1 先添加到t_logininfo表
        logininfoMapper.save(logininfo);
        //3.2 添加到t_user表
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private User mapEmailUser(String email, String password) {
        User user = new User();
        user.setUsername(email);
        user.setEmail(email);
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5Pwd = Md5Utils.encrypByMd5(password + salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }

}
